{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**这个程序主要是排查，在每个推荐的订单中，是否存在很大概率不是这个period_name点击的spu，例如中午时间就点了一个饼，这个就可以过滤掉**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from collections import defaultdict\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>wm_order_id</th>\n",
       "      <th>aor_id</th>\n",
       "      <th>order_timestamp</th>\n",
       "      <th>ord_period_name</th>\n",
       "      <th>aoi_id</th>\n",
       "      <th>takedlvr_aoi_type_name</th>\n",
       "      <th>dt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>185590</td>\n",
       "      <td>1110846</td>\n",
       "      <td>10</td>\n",
       "      <td>1624847527</td>\n",
       "      <td>1</td>\n",
       "      <td>920.0</td>\n",
       "      <td>11</td>\n",
       "      <td>20210628</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>8135</td>\n",
       "      <td>1083554</td>\n",
       "      <td>8</td>\n",
       "      <td>1624886397</td>\n",
       "      <td>4</td>\n",
       "      <td>586.0</td>\n",
       "      <td>0</td>\n",
       "      <td>20210628</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>170046</td>\n",
       "      <td>1114318</td>\n",
       "      <td>9</td>\n",
       "      <td>1624852169</td>\n",
       "      <td>1</td>\n",
       "      <td>1230.0</td>\n",
       "      <td>5</td>\n",
       "      <td>20210628</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>82768</td>\n",
       "      <td>1099638</td>\n",
       "      <td>2</td>\n",
       "      <td>1624849518</td>\n",
       "      <td>1</td>\n",
       "      <td>1362.0</td>\n",
       "      <td>3</td>\n",
       "      <td>20210628</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>45646</td>\n",
       "      <td>1084363</td>\n",
       "      <td>8</td>\n",
       "      <td>1624850242</td>\n",
       "      <td>1</td>\n",
       "      <td>561.0</td>\n",
       "      <td>0</td>\n",
       "      <td>20210628</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   user_id  wm_order_id  aor_id  order_timestamp  ord_period_name  aoi_id  \\\n",
       "0   185590      1110846      10       1624847527                1   920.0   \n",
       "1     8135      1083554       8       1624886397                4   586.0   \n",
       "2   170046      1114318       9       1624852169                1  1230.0   \n",
       "3    82768      1099638       2       1624849518                1  1362.0   \n",
       "4    45646      1084363       8       1624850242                1   561.0   \n",
       "\n",
       "  takedlvr_aoi_type_name        dt  \n",
       "0                     11  20210628  \n",
       "1                      0  20210628  \n",
       "2                      5  20210628  \n",
       "3                      3  20210628  \n",
       "4                      0  20210628  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test = pd.read_table('../../data/orders_test_spu.txt',sep='\\t')\n",
    "test.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>avg_pay_amt</th>\n",
       "      <th>avg_pay_amt_weekdays</th>\n",
       "      <th>avg_pay_amt_weekends</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>[36,49)</td>\n",
       "      <td>[36,49)</td>\n",
       "      <td>[36,49)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>[29,36)</td>\n",
       "      <td>[29,36)</td>\n",
       "      <td>&lt;29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>&gt;=65</td>\n",
       "      <td>&gt;=65</td>\n",
       "      <td>&gt;=65</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>&lt;29</td>\n",
       "      <td>&lt;29</td>\n",
       "      <td>&lt;29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>[29,36)</td>\n",
       "      <td>[29,36)</td>\n",
       "      <td>未知</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   user_id avg_pay_amt avg_pay_amt_weekdays avg_pay_amt_weekends\n",
       "0        0     [36,49)              [36,49)              [36,49)\n",
       "1        1     [29,36)              [29,36)                  <29\n",
       "2        2        >=65                 >=65                 >=65\n",
       "3        3         <29                  <29                  <29\n",
       "4        4     [29,36)              [29,36)                   未知"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "users = pd.read_table('../../data/users.txt')\n",
    "users.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['[36,49)', '[29,36)', '>=65', '<29', '未知', '[49,65)'], dtype=object)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "users['avg_pay_amt_weekdays'].unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>wm_food_spu_id</th>\n",
       "      <th>wm_food_spu_name</th>\n",
       "      <th>price</th>\n",
       "      <th>category</th>\n",
       "      <th>ingredients</th>\n",
       "      <th>taste</th>\n",
       "      <th>stand_food_id</th>\n",
       "      <th>stand_food_name</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>833366-bf3929-254096</td>\n",
       "      <td>26.0</td>\n",
       "      <td>[0]</td>\n",
       "      <td>[0, 1, 2]</td>\n",
       "      <td>[0]</td>\n",
       "      <td>0.0</td>\n",
       "      <td>833366-bf3929-254096</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>032521-a0772-3f3352</td>\n",
       "      <td>20.0</td>\n",
       "      <td>[1]</td>\n",
       "      <td>[3, 1]</td>\n",
       "      <td>[1]</td>\n",
       "      <td>1.0</td>\n",
       "      <td>032521-a0772-3f3352</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>952431-c52358-c22123-7f760-832660-fe1862</td>\n",
       "      <td>18.0</td>\n",
       "      <td>[2]</td>\n",
       "      <td>[3, 1, 4, 5, 6, 7]</td>\n",
       "      <td>[2]</td>\n",
       "      <td>2.0</td>\n",
       "      <td>952431-c52358-c22123-7f760-832660</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>e84149-6b1215-ca2776-441372-b5649-ac1915-f4338...</td>\n",
       "      <td>10.0</td>\n",
       "      <td>[3]</td>\n",
       "      <td>[8, 9]</td>\n",
       "      <td>[0]</td>\n",
       "      <td>3.0</td>\n",
       "      <td>e84149-6b1215-ca2776-441372</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>6b1215-c9725-862243-102951-1f1116</td>\n",
       "      <td>3.58</td>\n",
       "      <td>[4]</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   wm_food_spu_id                                   wm_food_spu_name price  \\\n",
       "0               0                               833366-bf3929-254096  26.0   \n",
       "1               1                                032521-a0772-3f3352  20.0   \n",
       "2               2           952431-c52358-c22123-7f760-832660-fe1862  18.0   \n",
       "3               3  e84149-6b1215-ca2776-441372-b5649-ac1915-f4338...  10.0   \n",
       "4               4                  6b1215-c9725-862243-102951-1f1116  3.58   \n",
       "\n",
       "  category         ingredients taste  stand_food_id  \\\n",
       "0      [0]           [0, 1, 2]   [0]            0.0   \n",
       "1      [1]              [3, 1]   [1]            1.0   \n",
       "2      [2]  [3, 1, 4, 5, 6, 7]   [2]            2.0   \n",
       "3      [3]              [8, 9]   [0]            3.0   \n",
       "4      [4]                 NaN   NaN            NaN   \n",
       "\n",
       "                     stand_food_name  \n",
       "0               833366-bf3929-254096  \n",
       "1                032521-a0772-3f3352  \n",
       "2  952431-c52358-c22123-7f760-832660  \n",
       "3        e84149-6b1215-ca2776-441372  \n",
       "4                                NaN  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "spus = pd.read_table('../../data/spus.txt')\n",
    "spus.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "order_rec_spus = {}\n",
    "for line in open('./submit-mean_score_50spu_15.txt'):\n",
    "    lines = [int(a) for a in line.strip().split('\\t')]\n",
    "    order_rec_spus[lines[0]] = lines[1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 想到一个很好的思路，就是把每个spu所属的aor表示一下。还有spu被那个时段点击的概率做成字典，这样有助于后面过滤"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.read_table('../data/three_week_data.txt',sep='\\t')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>wm_order_id</th>\n",
       "      <th>wm_poi_id</th>\n",
       "      <th>aor_id</th>\n",
       "      <th>order_price_interval</th>\n",
       "      <th>order_timestamp</th>\n",
       "      <th>ord_period_name</th>\n",
       "      <th>order_scene_name</th>\n",
       "      <th>aoi_id</th>\n",
       "      <th>takedlvr_aoi_type_name</th>\n",
       "      <th>dt</th>\n",
       "      <th>wm_food_spu_id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>178557</td>\n",
       "      <td>0</td>\n",
       "      <td>2334</td>\n",
       "      <td>6</td>\n",
       "      <td>&lt;29</td>\n",
       "      <td>1623061539</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>未知</td>\n",
       "      <td>20210607</td>\n",
       "      <td>39803</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>175118</td>\n",
       "      <td>1</td>\n",
       "      <td>3315</td>\n",
       "      <td>0</td>\n",
       "      <td>&lt;29</td>\n",
       "      <td>1623032193</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>20210607</td>\n",
       "      <td>61775</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>175118</td>\n",
       "      <td>1</td>\n",
       "      <td>3315</td>\n",
       "      <td>0</td>\n",
       "      <td>&lt;29</td>\n",
       "      <td>1623032193</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>20210607</td>\n",
       "      <td>49467</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>175118</td>\n",
       "      <td>1</td>\n",
       "      <td>3315</td>\n",
       "      <td>0</td>\n",
       "      <td>&lt;29</td>\n",
       "      <td>1623032193</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>20210607</td>\n",
       "      <td>15399</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>36208</td>\n",
       "      <td>2</td>\n",
       "      <td>2168</td>\n",
       "      <td>0</td>\n",
       "      <td>&lt;29</td>\n",
       "      <td>1623036350</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>20210607</td>\n",
       "      <td>21770</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>36208</td>\n",
       "      <td>2</td>\n",
       "      <td>2168</td>\n",
       "      <td>0</td>\n",
       "      <td>&lt;29</td>\n",
       "      <td>1623036350</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>20210607</td>\n",
       "      <td>160877</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>102798</td>\n",
       "      <td>3</td>\n",
       "      <td>3071</td>\n",
       "      <td>0</td>\n",
       "      <td>[29,36)</td>\n",
       "      <td>1623071723</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0</td>\n",
       "      <td>20210607</td>\n",
       "      <td>4702</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>102798</td>\n",
       "      <td>3</td>\n",
       "      <td>3071</td>\n",
       "      <td>0</td>\n",
       "      <td>[29,36)</td>\n",
       "      <td>1623071723</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0</td>\n",
       "      <td>20210607</td>\n",
       "      <td>74068</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>73712</td>\n",
       "      <td>4</td>\n",
       "      <td>2902</td>\n",
       "      <td>0</td>\n",
       "      <td>[49,65)</td>\n",
       "      <td>1623020472</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>20210607</td>\n",
       "      <td>119746</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>73712</td>\n",
       "      <td>4</td>\n",
       "      <td>2902</td>\n",
       "      <td>0</td>\n",
       "      <td>[49,65)</td>\n",
       "      <td>1623020472</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>20210607</td>\n",
       "      <td>179524</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   user_id  wm_order_id  wm_poi_id  aor_id order_price_interval  \\\n",
       "0   178557            0       2334       6                  <29   \n",
       "1   175118            1       3315       0                  <29   \n",
       "2   175118            1       3315       0                  <29   \n",
       "3   175118            1       3315       0                  <29   \n",
       "4    36208            2       2168       0                  <29   \n",
       "5    36208            2       2168       0                  <29   \n",
       "6   102798            3       3071       0              [29,36)   \n",
       "7   102798            3       3071       0              [29,36)   \n",
       "8    73712            4       2902       0              [49,65)   \n",
       "9    73712            4       2902       0              [49,65)   \n",
       "\n",
       "   order_timestamp  ord_period_name order_scene_name  aoi_id  \\\n",
       "0       1623061539                3                0     NaN   \n",
       "1       1623032193                1                1     0.0   \n",
       "2       1623032193                1                1     0.0   \n",
       "3       1623032193                1                1     0.0   \n",
       "4       1623036350                1                0     1.0   \n",
       "5       1623036350                1                0     1.0   \n",
       "6       1623071723                4                0     2.0   \n",
       "7       1623071723                4                0     2.0   \n",
       "8       1623020472                0                2     3.0   \n",
       "9       1623020472                0                2     3.0   \n",
       "\n",
       "  takedlvr_aoi_type_name        dt  wm_food_spu_id  \n",
       "0                     未知  20210607           39803  \n",
       "1                      0  20210607           61775  \n",
       "2                      0  20210607           49467  \n",
       "3                      0  20210607           15399  \n",
       "4                      0  20210607           21770  \n",
       "5                      0  20210607          160877  \n",
       "6                      0  20210607            4702  \n",
       "7                      0  20210607           74068  \n",
       "8                      1  20210607          119746  \n",
       "9                      1  20210607          179524  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "order_spu = defaultdict(list)\n",
    "order_spu_num = {} \n",
    "spu_order_num = defaultdict(list) # 存放每个spu被购买的时候一个订单中包含多少spu\n",
    "for order, spu in zip(data['wm_order_id'], data['wm_food_spu_id']):\n",
    "    order_spu[order].append(spu)\n",
    "for order in order_spu:\n",
    "    order_spu_num[order] = len(order_spu[order])\n",
    "for order in order_spu:\n",
    "    for spu in order_spu[order]:\n",
    "        spu_order_num[spu].append(order_spu_num[order])\n",
    "for spu in spu_order_num:\n",
    "    spu_order_num[spu] = list(set(spu_order_num[spu]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "def zero():\n",
    "    return 0\n",
    "## 统计测试集中order的spu数量\n",
    "test_order_spu_num = defaultdict(zero)\n",
    "for order in test['wm_order_id']:\n",
    "    test_order_spu_num[order] += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.21030116764761725\n"
     ]
    }
   ],
   "source": [
    "total = 0\n",
    "one_num = 0\n",
    "for order in test_order_spu_num:\n",
    "    total += 1\n",
    "    if test_order_spu_num[order] == 1:\n",
    "        one_num += 1\n",
    "print(one_num/total)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 下面就是通过设计，统计了每种spu各个时间点的购买次数\n",
    "def first_list():\n",
    "    return [0,0,0,0,0]\n",
    "spu_period_dict = defaultdict(first_list)\n",
    "for spu, period in zip(data['wm_food_spu_id'], data['ord_period_name']):\n",
    "    spu_period_dict[spu][period] += 1\n",
    "\n",
    "# 转换成概率的形式，其实也是归一化\n",
    "spu_period_prob_dict = {}\n",
    "for spu in spu_period_dict:\n",
    "    spu_period_prob_dict[spu] = np.array(spu_period_dict[spu]) / sum(spu_period_dict[spu])\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 下面就是通过设计，统计了每种spu各个aor的购买次数\n",
    "def aor_list():\n",
    "    return [0]*11\n",
    "spu_aor_dict = defaultdict(aor_list)\n",
    "for spu, aor in zip(data['wm_food_spu_id'], data['aor_id']):\n",
    "    spu_aor_dict[spu][aor] += 1\n",
    "\n",
    "# 转换成概率的形式，其实也是归一化\n",
    "spu_aor_prob_dict = {}\n",
    "for spu in spu_aor_dict:\n",
    "    spu_aor_prob_dict[spu] = np.array(spu_aor_dict[spu]) / sum(spu_aor_dict[spu])\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 下面就是通过设计，统计了每种spu各个takedlvr_aoi_type_name的购买次数\n",
    "def takedlvr_list():\n",
    "    return [0]*12\n",
    "spu_takedlvr_dict = defaultdict(takedlvr_list)\n",
    "for spu, takedlvr in zip(data['wm_food_spu_id'], data['takedlvr_aoi_type_name']):\n",
    "    if takedlvr != '未知':\n",
    "        spu_takedlvr_dict[spu][int(takedlvr)] += 1\n",
    "\n",
    "# 转换成概率的形式，其实也是归一化\n",
    "spu_takedlvr_prob_dict = {}\n",
    "for spu in spu_takedlvr_dict:\n",
    "    spu_takedlvr_prob_dict[spu] = np.array(spu_takedlvr_dict[spu]) / sum(spu_takedlvr_dict[spu])\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "user_spu = defaultdict(list) # 记录用户所有曾经购买的spu\n",
    "for user, spu in zip(data['user_id'], data['wm_food_spu_id']):\n",
    "    user_spu[user].append(spu)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "spu_num = defaultdict(zero)\n",
    "user_spu_num = {}\n",
    "for user in user_spu:\n",
    "    for spu in user_spu[user]:\n",
    "        spu_num[spu] += 1\n",
    "    user_spu_num[user] = spu_num\n",
    "    spu_num = defaultdict(zero)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "unique_test = test[-test.duplicated(['wm_order_id'])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "过滤替换掉不匹配的spu数量： 1107743\n",
      "过滤替换掉不匹配的spu比例： 0.5754311800218798\n"
     ]
    }
   ],
   "source": [
    "# 看看有多少概率上不属于这个时间点购买的spu\n",
    "period_threshold = 0.01\n",
    "aor_threshold = 0\n",
    "takedlvr_threshold = 0\n",
    "\n",
    "total = err_num = 0\n",
    "filter_period_dict = defaultdict(list)\n",
    "for order, period, aor, takedlvr, user in zip(unique_test['wm_order_id'],unique_test['ord_period_name'],unique_test['aor_id'],unique_test['takedlvr_aoi_type_name'],unique_test['user_id']):\n",
    "    rec_num = 0\n",
    "    for spu in order_rec_spus[order][:50]:\n",
    "        total += 1\n",
    "        if takedlvr == '未知' or spu not in spu_takedlvr_prob_dict:\n",
    "            if spu_period_prob_dict[spu][period] > period_threshold and spu_aor_prob_dict[spu][aor] > aor_threshold and test_order_spu_num[order] >= min(spu_order_num[spu]) :\n",
    "                filter_period_dict[order].append(spu)\n",
    "                rec_num +=1 \n",
    "                if rec_num == 5:\n",
    "                    break\n",
    "            else:\n",
    "                err_num += 1\n",
    "        else:\n",
    "            if spu_period_prob_dict[spu][period] > period_threshold and spu_aor_prob_dict[spu][aor] > aor_threshold and spu_takedlvr_prob_dict[spu][int(takedlvr)]>takedlvr_threshold and test_order_spu_num[order] >= min(spu_order_num[spu]) : \n",
    "                filter_period_dict[order].append(spu)\n",
    "                rec_num +=1 \n",
    "                if rec_num == 5:\n",
    "                    break\n",
    "            else:\n",
    "                err_num += 1\n",
    "print('过滤替换掉不匹配的spu数量：', err_num) \n",
    "print('过滤替换掉不匹配的spu比例：', err_num/total)  #  0.5780"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "订单推荐的菜品不足5个的有： 13297\n"
     ]
    }
   ],
   "source": [
    "# 过滤后的结果输出到文件中，并且输出不足5个菜品的个数\n",
    "with open('filter_period_submit-mean_score_17.txt','w') as f: \n",
    "    num = 0\n",
    "    for order in test['wm_order_id']:\n",
    "        f.write(str(order))\n",
    "        count = 0\n",
    "        for spu in filter_period_dict[order]:\n",
    "            f.write('\\t')\n",
    "            f.write(str(spu))\n",
    "            count += 1\n",
    "        if count != 5:\n",
    "            num += 1\n",
    "        f.write('\\n')\n",
    "    print('订单推荐的菜品不足5个的有：',num) # 13550"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>154263</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>135230</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>53579</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>90957</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>12091</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        0\n",
       "0  154263\n",
       "1  135230\n",
       "2   53579\n",
       "3   90957\n",
       "4   12091"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# \n",
    "recall_items = pd.read_table('../../recall_items_5w.txt',header=None)\n",
    "recall_items.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 在召回的物品中筛选出满足特定条件的菜品\n",
    "flag = False\n",
    "period_threshold = 0.4 \n",
    "aor_threshold = 0\n",
    "period_threshold_spu = {0:[],1:[],2:[],3:[],4:[]}\n",
    "\n",
    "for spu in recall_items[0]:\n",
    "    for period in period_threshold_spu:\n",
    "        if spu_period_prob_dict[spu][period] > period_threshold:\n",
    "            period_threshold_spu[period].append(spu)\n",
    "            break\n",
    "    for period in period_threshold_spu:\n",
    "        if len(period_threshold_spu[period]) !=5:\n",
    "            flag = False\n",
    "            break\n",
    "        else:\n",
    "            flag = True\n",
    "    if flag:\n",
    "        break  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 4271\n",
      "1 26559\n",
      "2 506\n",
      "3 10247\n",
      "4 1394\n"
     ]
    }
   ],
   "source": [
    "for period in period_threshold_spu:\n",
    "    print(period, len(period_threshold_spu[period]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "存在订单没有被补全为top-5，数量： 0\n"
     ]
    }
   ],
   "source": [
    "# 对前面过滤后不足5个菜品的订单进行补全\n",
    "err_num = 0\n",
    "with open('recover_best_result.txt','w') as f:\n",
    "    for order, period, aor, takedlvr in zip(test['wm_order_id'],test['ord_period_name'],test['aor_id'],test['takedlvr_aoi_type_name']):\n",
    "        f.write(str(order))\n",
    "        count = 0\n",
    "        for spu in filter_period_dict[order]:\n",
    "            f.write('\\t')\n",
    "            f.write(str(spu))\n",
    "            count += 1\n",
    "        if count != 5:\n",
    "            flag = False\n",
    "            for spu in period_threshold_spu[period]:\n",
    "                if spu not in filter_period_dict[order] and spu_aor_prob_dict[spu][aor] > 0 and (takedlvr=='未知' or spu_takedlvr_prob_dict[spu][int(takedlvr)]>0) and test_order_spu_num[order] >= min(spu_order_num[spu]) :\n",
    "                    f.write('\\t')\n",
    "                    f.write(str(spu))\n",
    "                    count += 1\n",
    "                if count == 5:\n",
    "                    flag = True\n",
    "                    break\n",
    "            if flag == False:\n",
    "                err_num +=1\n",
    "        f.write('\\n')\n",
    "print('存在订单没有被补全为top-5，数量：',err_num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
